<script lang="ts" setup>
import { Delete, Plus } from '@element-plus/icons-vue'
import { UploadProps } from 'element-plus'
import { computed } from 'vue'

const $baseMessage = inject('$baseMessage') as any
const isUpload = ref(false)
const baseUrl = `${import.meta.env.VITE_APP_BASE_URL}`

const emit = defineEmits(['update:modelValue'])

const props = withDefaults(
  defineProps<{
    multiple?: boolean
    modelValue: string[] | string
  }>(),
  {
    multiple: false,
  }
)

const handleBeforeUpload: UploadProps['beforeUpload'] = () => {
  isUpload.value = true
  return true
}

const handleUploadSuccess: UploadProps['onSuccess'] = (response) => {
  const { url } = response
  if (props.multiple) {
    const arr = [...props.modelValue, url]
    imageUrl.value = arr
  } else {
    imageUrl.value = url
  }
  isUpload.value = false
  $baseMessage('上传成功', 'success', 'hey')
}

const handleDelete = (index: number) => {
  if (props.multiple) {
    const arr = [...imageUrl.value]
    arr.splice(index, 1)
    imageUrl.value = arr
  } else {
    imageUrl.value = ''
  }
}

const handleUploadError: UploadProps['onError'] = () => {
  isUpload.value = false
  $baseMessage('上传失败', 'error', 'hey')
}

const imageUrl = computed({
  get: () => props.modelValue,
  set: (val) => {
    emit('update:modelValue', val)
  },
})
</script>

<template>
  <div class="flex flex-wrap">
    <el-upload
      accept="image/*"
      :action="`${baseUrl}/api/ossPut`"
      :before-upload="handleBeforeUpload"
      class="avatar-uploader"
      :disabled="isUpload"
      :on-error="handleUploadError"
      :on-success="handleUploadSuccess"
      :show-file-list="false"
    >
      <el-icon v-loading="isUpload" class="avatar-uploader-icon">
        <Plus v-if="!isUpload" />
      </el-icon>
    </el-upload>
    <template v-if="typeof imageUrl == 'object'">
      <div v-show="imageUrl.length" class="flex flex-wrap">
        <div v-for="(url, index) in imageUrl" :key="url" class="image">
          <div class="image-box relative">
            <img :src="url" />
            <div class="mask flex-center text-20px c-white">
              <el-icon class="cursor-pointer" @click.stop="handleDelete(index)">
                <Delete />
              </el-icon>
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-else>
      <div v-if="imageUrl" class="image">
        <div class="image-box relative">
          <img :src="imageUrl" />
          <div class="mask flex-center text-20px c-white">
            <el-icon class="cursor-pointer" @click.stop="handleDelete(0)">
              <Delete />
            </el-icon>
          </div>
        </div>
      </div>
    </template>
  </div>
</template>

<style lang="scss" scoped>
.image {
  border: 1px solid var(--el-border-color);
  border-radius: 6px;
  font-size: 0;
  margin-right: 10px;
  margin-bottom: 10px;
  transition: var(--el-transition-duration-fast);

  .image-box {
    border-radius: 6px;
    width: 80px;
    height: 80px;
    transition: var(--el-transition-duration-fast);
    &:hover {
      .mask {
        opacity: 1;
        transition: opacity var(--el-transition-duration);
      }
    }

    img {
      border-radius: 6px;
      font-size: 0;
      width: 80px;
      height: 80px;
      object-fit: contain;
    }
    .mask {
      position: absolute;
      top: 0;
      left: 0;
      width: 80px;
      height: 80px;
      background-color: rgba(0, 0, 0, 0.5);
      border-radius: 6px;
      opacity: 0;
      transition: opacity var(--el-transition-duration);
    }
  }
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
  margin-right: 10px;
  margin-bottom: 10px;
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  text-align: center;
}
</style>
